﻿using System;
using System.Web.Configuration;
using Microsoft.Practices.Unity;

namespace SharedGenomics.Core.Configuration
{
    public class WebConfigurationReaderFactory : ConfigReaderFactory
    {
        private readonly string _webConfigPath;

        public WebConfigurationReaderFactory()
            : base()
        {
        }

        public WebConfigurationReaderFactory(bool provideSettings)
            : base(provideSettings)
        {
        }

        public WebConfigurationReaderFactory(string webConfigPath)
        {
            this._webConfigPath = webConfigPath;
        }

        protected override IConfigurationReader GetConfiguration()
        {
            if (this._webConfigPath == null)
            {
                return new DefaultConfigurationReader(WebConfigurationManager.AppSettings, WebConfigurationManager.ConnectionStrings);
            }
            else
            {
                var config = WebConfigurationManager.OpenWebConfiguration(this._webConfigPath);
                return new InstanceConfigurationReader(config.AppSettings, config.ConnectionStrings);
            }
        }
    }
}
